//
// Created by madison on 2022/6/25.
//

#include <vector>

using namespace std;

class Solution {
public:
    void moveZeroes(vector<int> &nums) {
        if (nums.size() < 0) {
            return;
        }
        int index = 0;
        // 一次遍历，把非零的都往前挪
        for (int i = 0; i < nums.size(); ++i) {
            if (nums[i] != 0) {
                nums[index++] = nums[i];
            }
        }
        while (index < nums.size()) {
            nums[index++] = 0;
        }
    }

    void moveZeroes1(vector<int> &nums) {
        int i = 0;//统计前面0的个数
        for (int j = 0; j < nums.size(); ++j) {
            if (nums[j] == 0) {//如果当前数字是0就不操作
                i++;
            } else if (i != 0) {
                //否则，把当前数字放到最前面那个0的位置，然后再把
                //当前位置设为0
                nums[j - i] = nums[j];
                nums[j] = 0;
            }
        }
    }

    void moveZeroes2(vector<int> &nums) {
        int n = nums.size(), left = 0, right = 0;
        while (right < n) {
            if (nums[right]) {
                swap(nums[left], nums[right]);
                left++;
            }
            right++;
        }
    }
};

int main() {
    Solution solution;
    vector<int> nums = {0, 1, 0, 3, 12};
    solution.moveZeroes2(nums);
    return 0;
}